# data feeds
import datetime
import backtrader as bt
import backtrader.feeds as btfeeds

from strategies.sma import SmaCross

# 從Yahoo Finance取得資料
data = btfeeds.YahooFinanceCSVData(
    # proxies={'http': 'http://127.0.0.1:18640'},
    dataname='./data/yahoo/NVDA_stock_data_2023.csv',
)
# data = btfeeds.YahooFinanceData(
#                                 # proxies={'http': 'http://127.0.0.1:18640'},
#                                 dataname='AAPL',
#                                 fromdate=datetime.datetime(2019, 1, 1),
#                                 todate=datetime.datetime(2019, 12, 31))
print(data)


if __name__ == '__main__':
    # 初始化cerebro
    cerebro = bt.Cerebro()

    cerebro.broker.setcash(100000.0)
    # feed data
    cerebro.adddata(data)
    # add strategy
    cerebro.addstrategy(SmaCross)

    start_cash = cerebro.broker.getvalue()
    # run backtest
    cerebro.run()
    print('------------  策略回测总结 start -----------')
    print('----- 期初资产额: %.2f' % start_cash)
    print('----- 期末资产额: %.2f' % cerebro.broker.getvalue())
    #计算策略年化收益率
    print('----- 年化收益率: %.2f%%' % (cerebro.broker.getvalue() / 100000.0 * 12 - 1))
    print('------------  策略回测总结 end -----------')
    # plot diagram
    cerebro.plot()